//
//  JMCheckOutVersion.m
//  JMDome_1
//
//  Created by ios on 15/6/30.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMCheckOutVersion.h"
#import "NSString+date.h"

@interface JMCheckOutVersion()<UIAlertViewDelegate>{


}

-(void) checkOutAppVersionWithDay;

-(void) checkOutAppVersionWithWeek;

- (NSString *)getLocationVersionNumber;

- (NSString *)getServerVersionNumber;

@end


@implementation JMCheckOutVersion
@synthesize type = _type;


static JMCheckOutVersion * _checkOutVresion = nil;
+ (instancetype)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _checkOutVresion = [[JMCheckOutVersion alloc]init];
    });
    return _checkOutVresion;
}
-(id)init{
    self = [super init];
    if(self){
        _type = CHeckVersionTypeDefault;
        NSUserDefaults *def =[NSUserDefaults standardUserDefaults];
        NSString * timeString = [NSString dateWithSting:[NSDate date]];//[self.formatter stringFromDate:[NSDate date]];
        [def setObject:timeString forKey:LASTUPTIME];
        [def synchronize];
    }
    return self;
}
-(void)versionHelp{
    
    switch (_type) {
        case 0:{
            [self checkOutAppVersion];
        }break;
        case 1:{
            [self checkOutAppVersionWithDay];
        }break;
        case 2:{
            [self checkOutAppVersionWithWeek];
        }break;
        default:{
            @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"这个方法里，更新类型出错%@",NSStringFromSelector(_cmd)] userInfo:nil];
            //_type 类型出错抛出警告信息
        }break;
    }
}

-(void) checkOutAppVersionWithDay{
    NSUserDefaults *def =[NSUserDefaults standardUserDefaults];
    NSString * lastupTime = [def objectForKey:LASTUPTIME];
    NSString * timeString = [NSString dateWithSting:[NSDate date]];//[self.formatter stringFromDate:[NSDate date]];
    if(![lastupTime isEqualToString:timeString]){
        [self checkOutAppVersion];
    }
}

-(void) checkOutAppVersionWithWeek{

}

- (NSString *)getLocationVersionNumber{
    NSString *version = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleVersion"];
    return version;
}

- (NSString *)getServerVersionNumber{
    NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunesconnect.apple.com/lookup?id=%@",APPKEYID]] encoding:NSUTF8StringEncoding error:nil];
    if(string != nil && string.length > 0 &&[string rangeOfString:@"version"].length == 7){
        NSString * version = [string substringToIndex:[string rangeOfString:@","].length  + 10];
        return version;
    }
    return nil;
}

-(void)checkOutAppVersion{
    NSString * version_loc = [self getLocationVersionNumber];
    NSString * version_serv= [self getServerVersionNumber];
    if(version_serv == nil)return;
    if(![version_serv isEqualToString:version_loc]){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:[NSString stringWithFormat:@"新版本 %@ 已发布",version_serv] delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
        alert.delegate =self;
        [alert addButtonWithTitle:@"前往更新"];
        [alert show];
        alert.tag = 30;
    }
}

#pragma mark alert view  que ding  qu geng xin
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 1 && alertView.tag == 30){
        NSString *url = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@", APPKEYID];
        NSURL *iTunesURL = [NSURL URLWithString:url];
        [[UIApplication sharedApplication] openURL:iTunesURL];
    }
}

- (void)setType:(CHeckVersionType)type{
    if(_type != type){
        _type = type;
    }
}
-(CHeckVersionType )type{
    return _type;
}
@end
